/**
 * Created by gongxun on 17/2/8.
 */

var KwxTablePlayerInfoLayer = GameCCSBase.extend({
    Panel_info : null,
    Panel_action : null,
    Image_infoMy : null,
    Image_infoOther : null,

    mSeat : null,
    mUserInfo : null,
    mLogicSeat : null,

    mButton_effects : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxTablePlayerInfo.json");

        cc.spriteFrameCache.addSpriteFrames("res/bomb.plist", "res/bomb.png");
        cc.spriteFrameCache.addSpriteFrames("res/egg.plist", "res/egg.png");
        cc.spriteFrameCache.addSpriteFrames("res/flower.plist", "res/flower.png");
        cc.spriteFrameCache.addSpriteFrames("res/shoe.plist", "res/shoe.png");
        cc.spriteFrameCache.addSpriteFrames("res/zan.plist", "res/zan.png");

        this.Panel_info = mo.seekWidgetByName(this.ccsNode, "Panel_info");
        this.Panel_action = mo.seekWidgetByName(this.ccsNode, "Panel_action");
        this.Image_infoMy = mo.seekWidgetByName(this.ccsNode, "Image_infoMy");
        this.Image_infoOther = mo.seekWidgetByName(this.ccsNode, "Image_infoOther");

        this.Panel_info.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.closePlayerInfo();
            }
        }, this);

        //send
        this.mButton_effects = [];
        for (var i = 0; i < 5; i++){
            var Button_effect = mo.seekWidgetByName(this.ccsNode, "Button_effect" + i);
            this.mButton_effects.push(Button_effect);
            Button_effect.setTag(i);

            Button_effect.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    this.sendEffectChat(sender.getTag());
                    this.closePlayerInfo();
                }
            }, this);
        }

        this.closePlayerInfo();
    },

    showPlayerInfo : function(seat, userinfo, startPos, logicSeat){
        this.Panel_info.setVisible(true);
        this.Image_infoMy.stopAllActions();
        this.Image_infoMy.setVisible(false);
        this.Image_infoOther.stopAllActions();
        this.Image_infoOther.setVisible(false);

        this.mSeat = seat;
        this.mUserInfo = userinfo;
        this.mLogicSeat = logicSeat;

        var ui;
        if (seat == 0){
            ui = this.Image_infoMy;
        }else{
            ui = this.Image_infoOther;
        }
        this.updateUserInfo(ui);

        ui.setScale(0.1);
        ui.setPosition(startPos);
        ui.setVisible(true);

        ui.runAction(cc.sequence(cc.moveTo(0.2, cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2))));
        ui.runAction(cc.sequence(cc.scaleTo(0.2, 1)));
    },

    closePlayerInfo : function(){
        this.Panel_info.setVisible(false);
    },

    updateUserInfo : function(ui){
        var deviceJson = {};
        if (mo.gameHelper.getIsJson(this.mUserInfo.DeviceJson)){
            deviceJson = JSON.parse(this.mUserInfo.DeviceJson);
        }

        mo.gameHelper.setTextString(mo.seekWidgetByName(ui, "Text_nick"), "" + this.mUserInfo.nickname);
        mo.gameHelper.setTextString(mo.seekWidgetByName(ui, "Text_id"), "" + this.mUserInfo.userid);
        mo.gameHelper.setTextString(mo.seekWidgetByName(ui, "Text_ip"), "" + deviceJson.ip);
        mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(ui, "Image_head"), this.mUserInfo.faceUrl);
        var sexImage = this.mUserInfo.sex == 0 ? "res/ccs/chateffect/male.png" : "res/ccs/chateffect/female.png";
        mo.gameHelper.loadTextureImage(mo.seekWidgetByName(ui, "Image_sex"), sexImage);

        //gps
        cc.log("DeviceJson:" + this.mUserInfo.DeviceJson);
        if (mo.gameHelper.getIsJson(this.mUserInfo.DeviceJson)){
            mo.seekWidgetByName(ui, "TextFull_gps").setString("" + deviceJson.gps);
            if (this.mSeat != 0 && !mo.gameUserInfo.getMyIsOpenPhoneLocation()){
                mo.seekWidgetByName(ui, "TextFull_gps").setString("没有获取到您的GPS定位，因此无法查看他人GPS定位");
            }
        }else{
            mo.seekWidgetByName(ui, "TextFull_gps").setString("");
        }

        if (mo.gameHelper.developerVersion == "3.0"){
            //荣誉
            mo.seekWidgetByName(ui, "Text_textHoner").setVisible(true);
            mo.seekWidgetByName(ui, "Text_honor").setVisible(true);
            var honor = mo.gameUserInfo.getItemNum(this.mUserInfo.items_id, this.mUserInfo.items_num, 20002);
            mo.gameHelper.setTextString(mo.seekWidgetByName(ui, "Text_honor"), "" + honor);

        }else if (mo.gameHelper.developerVersion == "2.0"){
            //荣誉
            mo.seekWidgetByName(ui, "Text_textHoner").setVisible(false);
            mo.seekWidgetByName(ui, "Text_honor").setVisible(false);
        }
    },

    sendEffectChat : function(index){
        var sendIndex = 1000;
        sendIndex += index * 10;
        sendIndex += this.mLogicSeat;
        mo.gameMsgManager.sendUserChatMsg(mo.gameUserInfo.uid, sendIndex.toString(), 1);

        for(var i = 0; i < this.mButton_effects.length; i++){
            var button_effect = this.mButton_effects[i];
            button_effect.stopAllActions();
            button_effect.setEnabled(false);
            button_effect.runAction(cc.sequence(
                cc.delayTime(2),
                cc.callFunc(function(){
                    this.setEnabled(true);
                }, button_effect)
            ));
        }
    },

    showEffectChat : function(formSeat, toSeat, index, p1, p2){
        cc.log("effectChat 1:" + formSeat + " 2:" + toSeat + " 3:" + index);
        var imageTemp = [
            "#eggtemp.png",
            "#shoetemp.png",
            "#bombtemp.png",
            "#zantemp.png",
            "#flowertemp.png"
        ];

        var imageStart = [
            "eggstart",
            "shoestart",
            "bombstart",
            "zanstart",
            "flowerstart"
        ];

        var imageEnd = [
            "eggend",
            "shoeend",
            "bombend",
            "zanend",
            "flowerend"
        ];

        var speed = 1500;
        var dis = cc.pDistance(p1, p2);
        var scaleTo = toSeat == 0 ? 2 : 1;
        if (toSeat == 0){
            p2 = cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2);
        }

        var sprite = new cc.Sprite(imageTemp[index]);
        sprite.setPosition(p1);
        sprite.setScale(0.6);
        this.addChild(sprite);


        var spriteFrames = [];
        for (var i = 1; i <= 30; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame(imageStart[index] + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        spriteFrames.push(cc.spriteFrameCache.getSpriteFrame(imageTemp[index].substring(1)));
        var animation1 = new cc.Animation(spriteFrames);
        animation1.setDelayPerUnit(0.05);

        spriteFrames = [];
        for (var i = 1; i <= 30; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame(imageEnd[index] + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        var animation2 = new cc.Animation(spriteFrames);
        animation2.setDelayPerUnit(0.1);

        sprite.runAction(cc.sequence(
            cc.animate(animation1),
            cc.moveTo(dis / speed, p2),
            cc.callFunc(function(){
                sprite.setScale(scaleTo);
            }),
            cc.animate(animation2),
            cc.removeSelf()
        ));
    }
});
